<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">

    /**
     * 自定义验证规则
     */
    function validateForm() {
        form.bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                imageInfo_1: {
                    validators: {
                        notEmpty: {message: "请上传图片！"},
                        file: {maxSize: 1024 * 1024, message: '请上传一张最大不超过1M的jpg/png/gif图片文件'}
                    }
                },
                imageInfo_2: {
                    validators: {
                        notEmpty: {message: "请上传图片！"},
                        file: {maxSize: 1024 * 1024, message: '请上传一张最大不超过1M的jpg/png/gif图片文件'}
                    }
                },
                promotionName: {
                    validators: {
                        notEmpty: {message: "活动主题不可为空"},
                        stringLength: {min:2,max: 100, message: "活动主题长度为2至100"}
                    }
                },
                beginTime: {
                    validators: {
                        notEmpty: {message: "开始时间不可为空"}
                    }
                },
                endTime: {
                    validators: {
                        notEmpty: {message: "结束时间不可为空"},
                        callback: {message: '结束时间应大于开始时间',
                            callback: function(value, validator) {
                                var beginTime=$("#beginTime").val();
                                if(new Date(value).valueOf() <= new Date(beginTime).valueOf()){
                                    return false;
                                }
                                return true;
                            }
                        }
                    }
                },
                shopPromotionInfo: {
                    validators: {
                        notEmpty: {message: "活动简介不可为空"}
                    }
                }
            }
        });
    }

    /**
     * 添加促销活动
     */
    <shiro:hasPermission name="back_gotoPromotionPage_add">
    function addInfo() {
        $("#myModalLabel").empty().text("添加促销活动");
        /**清空隐藏域的值**/
        $("#promotionId").val("");
        $("#imageUrl_1").val("");
        $("#imageUrl_2").val("");
        $("#createTime").val("");
        $("#updateTime").val("");
        $("#isPass").val("");
        $("#useStatus").val("");
        $("#checkTime").val("");
        $("#verifier").val("");

        /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
        initImageForAdd(1,"sys_shop", "image_promotion", 1, 1000, 300);
        initImageForAdd(2,"sys_shop", "image_promotion", 1, 600, 150);
        /**重置可见表单控件**/
        resetAddForm();
        /**初始化显示并控制时间选择器**/
        showStartDatePicker(new Date());
        showEndDatePicker(new Date())
    }
    </shiro:hasPermission>

    /**
     * 修改促销活动
     */
    <shiro:hasPermission name="back_gotoPromotionPage_update">
    function editInfo() {
        $("#myModalLabel").empty().text("修改促销活动");
        /**获取选中行的id**/
        var selectedRow = $("#tt").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            var id = selectedRow[0].promotionId;
            var currentTime = getCurrentTime();
            if (toJsonDate(selectedRow[0].beginTime,"yyyy-MM-dd hh:mm:ss") > currentTime){
                $("#btn_edit").attr("data-target", "#addOrEditModal");/**选中了记录后才可打开对应的模态框**/
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/promotion/getPlatformPromotionObject.sjson",
                    data: {promotionId: id},
                   /* async: false,*/
                    success: function (result) {
                        var data = JSON.parse(result.data);
                        /**隐藏域**/
                        $("#promotionId").val(data.promotionId);
                        if(data.createTime!=undefined && data.createTime!="") {
                            $("#createTime").val(new Date(data.createTime).Format("yyyy-MM-dd hh:mm:ss"));
                        }
                        if(data.updateTime!=undefined && data.updateTime!=""){
							$("#updateTime").val(new Date(data.updateTime).Format("yyyy-MM-dd hh:mm:ss"));
						}
                        $("#promotionNumber").val(data.promotionNumber);
                        $("#isPass").val(data.isPass);
                        $("#useStatus").val(data.useStatus);
                        if (data.checkTime!=undefined && data.checkTime!=""){
							$("#checkTime").val(new Date(data.checkTime).Format("yyyy-MM-dd hh:mm:ss"));
						}
                        $("#verifier").val(data.verifier);
                        $("#imageUrl_1").val(data.largePoster);
                        $("#imageUrl_2").val(data.smallPoster);

                        /**可见表单控件**/
                        $("#promotionName").val(data.promotionName);
                        if(data.beginTime!=undefined&&data.beginTime!="") {
                            $("#beginTime").val(new Date(data.beginTime).Format("yyyy-MM-dd hh:mm"));
                            /**显示并控制时间选择器**/
                            showStartDatePicker(new Date(data.beginTime).Format("yyyy-MM-dd hh:mm"));
                            showEndDatePicker(new Date(data.beginTime).Format("yyyy-MM-dd hh:mm"));
                        }
                        if(data.endTime!=undefined && data.endTime!="") {
                            $("#endTime").val(new Date(data.endTime).Format("yyyy-MM-dd hh:mm"));
                        }
                        $("#shopPromotionInfo").val(data.shopPromotionInfo);

                        /**回写预览上传文件所需的信息，如果有上传图片功能添加此代码，否则可以去掉**/
                        initImageForUpdate(1,"sys_shop", "image_promotion", 1, 1000, 300, "${uploadPath}", data.largePoster);
                        initImageForUpdate(2,"sys_shop", "image_promotion", 1, 600, 150, "${uploadPath}", data.smallPoster);
                        reValidateForm();
                        removeValidateField('imageInfo_1');
                        removeValidateField('imageInfo_2');

                    }
                });
			}else {
                $("#btn_edit").attr("data-target", "");/**时间已过期不可打开模态框**/
                showMsg("该促销活动开始时间已过，不能修改！");
            }
        } else {
            $("#btn_edit").attr("data-target", "");/**没有选中记录不可打开模态框**/
            showMsg("请选择一行记录！");
        }
    }
    </shiro:hasPermission>

    /**
	 * 1、控制开始时间和结束时间在表单中选择的逻辑
	 * 2、开始时间小于结束时间并且精确到分钟可选择
     */
    function showStartDatePicker(startDate){
		$("#beginTime").datetimepicker({
            format: 'yyyy-mm-dd hh:ii', showMeridian: true, language: 'zh-CN', autoclose: true, todayBtn: true, startDate: startDate
        }).on('changeDate', function(event_start) {
            /**指定表单字段更新成功的验证状态**/
            updateValidateFieldStatus('beginTime');
            showEndDatePicker(event_start.date);
            $("#endTime").val("");
            reValidateField( 'endTime');
        });
    }

    /**
     * 1、通过开始时间控制结束时间的展示
     */
    function showEndDatePicker(selectedStartDate){
        $("#endTime").datetimepicker({
            format: 'yyyy-mm-dd hh:ii', showMeridian: true, language: 'zh-CN', autoclose: true, todayBtn: true, startDate: selectedStartDate
        }).on('changeDate', function(event_end) {
            if (event_end.date.valueOf() > selectedStartDate.valueOf()){
                /**指定表单字段更新成功的验证状态**/
                updateValidateFieldStatus('endTime');
            }
        });
    }

    /**
     * 1、处理时间选择器精确到分钟的表单提交
	 * 2、在分钟后进行秒:00的追加处理
     */
    function submitPromotionForm() {
        /**1、对非空时间进行秒的处理**/
        var beginTime=$("#beginTime").val();
        var endTime=$("#endTime").val();
        if(beginTime!="" && endTime!=""){
            $("#beginTime").val(beginTime.substring(0,16)+":00");
            $("#endTime").val(endTime.substring(0,16)+":00");
        }
        submitForm();
    }

</script>

<!--添加编辑模态框（Modal） -->
<div class="modal fade" id="addOrEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel"></h5>
			</div>
			<div class="modal-body">
				<form id="form1" method="post" action="${basePath}/back/promotion/saveOrUpdatePlatformPromotion.sjson">
					<input id="promotionId" type="hidden" name="promotionId" />
					<input id="promotionNumber" type="hidden" name="promotionNumber" value=""/>
					<input id="createTime" type="hidden" name="createTime"/>
					<input id="updateTime" type="hidden"  name="updateTime"/>
					<input id="isPass" type="hidden" name="isPass" />
					<input id="useStatus" type="hidden" name="useStatus" value=""/>
					<input id="checkTime" type="hidden" name="checkTime" value=""/>
					<input id="verifier" type="hidden" name="verifier" value=""/>
					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>活动大海报:</td>
							<td class="toleft_td">
								<div class="container">
									<input id="imageUrl_1" type="hidden" name="largePoster" value=""/>
									<div class="form-group" id="fileWrap1"></div>
								</div>
								<span style="color:red">提示：请上传规格宽1000px，高300px的图片</span>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>活动小海报:</td>
							<td class="toleft_td">
								<div class="container">
									<input id="imageUrl_2" type="hidden" name="smallPoster" value=""/>
									<div class="form-group" id="fileWrap2"></div>
								</div>
								<span style="color:red">提示：请上传规格宽600px，高150px的图片</span>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>活动主题:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="promotionName" name="promotionName" placeholder="请输入促销活动名称"/>
								</div>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>活动开始时间:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<%--<div class="input-group date form_datetime_start col-md-8" data-link-field="dtp_input1">--%>
									<div class="form-group" style="">
										<input id="beginTime" name="beginTime" class="form-control" type="text" value="" placeholder="请选择活动开始时间" style="cursor: pointer;font-size: 12px;" readonly>
									</div>
										<%--<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
									<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
								</div>--%>
							</td>
							<td class="toright_td" width="150px"><span style="color:red">* </span>活动结束时间:&nbsp;&nbsp;</td>
							<td  class="toleft_td">
								<%--<div class="input-group date form_datetime_end col-md-8" data-link-field="dtp_input1">--%>
									<div class="form-group">
										<input id="endTime" name="endTime" class="form-control" type="text" value="" placeholder="请选择活动结束时间" style="cursor: pointer;font-size: 12px;" readonly>
									</div>
										<%--<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
									<span class="input-group-addon"><span class="glyphicon glyphicon-th"></span></span>
								</div>--%>
							</td>
						</tr>
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>活动简介:&nbsp;&nbsp;</td>
							<td class="toleft_td" colspan="3">
								<div class="form-group">
									<textarea class="form-control input-sm" type="text" id="shopPromotionInfo" name="shopPromotionInfo" placeholder="请输入促销活动详情"></textarea>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary" onclick="submitPromotionForm()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
